package com.ccsu.joysport.controller;

import com.ccsu.joysport.controller.model.resp.AdminActivityResp;
import com.ccsu.joysport.dto.*;
import com.ccsu.joysport.service.AdminActivityService;
import com.ccsu.joysport.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/ccsu/b/admin/activity")
public class AdminActivityController {

    @Autowired
    private AdminActivityService adminActivityService;

    @Autowired
    private JwtUtil jwtUtil;



    /**
     * 活动管理 - 列表 - 分页
     */
    @GetMapping("/manage/list")
    public List<AdminActivityResp> manageList(@RequestParam String adminToken,
                                        @RequestParam(required = false) String searchText,
                                        @RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(adminToken);
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "用户信息认证异常");
        }

        if (!jwtUtil.validateToken(adminToken)) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "无效的token");
        }

        return adminActivityService.manageList(adminId, searchText, pageNum - 1, pageSize);
    }

    /**
     * 活动审核 - 列表 - 分页
     */
    @GetMapping("/review/list")
    public List<AdminActivityResp> reviewList(@RequestParam String adminToken) {
        if (!jwtUtil.validateToken(adminToken)) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "无效的token");
        }

        return adminActivityService.reviewList();
    }

    //新增活动
    @PostMapping("/createActivity")
    public ResponseEntity<?> createActivity(@RequestBody CreateActivityRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ActivityResponse response = adminActivityService.createActivity(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //删除活动
    @DeleteMapping("/deleteActivity")
    public ResponseEntity<?> deleteActivity(@RequestBody DeleteActivityRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ActivityResponse response = adminActivityService.deleteActivity(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //活动签到
    @PostMapping("/checkInActivity")
    public ResponseEntity<?> checkInActivity(@RequestBody CheckInActivityRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        CheckInActivityResponse response = adminActivityService.checkInActivity(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //活动详情
    @GetMapping("/queryActivity")
    public ResponseEntity<?> queryActivity(@RequestParam String adminToken, @RequestParam String activityId) {
        if (!jwtUtil.validateToken(adminToken)) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ActivityWithClubResponse response = adminActivityService.queryActivity(activityId);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //活动报名的学生列表
    @GetMapping("/getSignupStudents")
    public ResponseEntity<?> getSignupStudents(@RequestParam String adminToken, @RequestParam String activityId) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(adminToken);
        } catch (Exception e) {
            return new ResponseEntity<>("token解析失败", HttpStatus.UNAUTHORIZED);
        }

        List<StudentSignupResponse> signupStudents = adminActivityService.getSignupStudents(activityId);
        return new ResponseEntity<>(signupStudents, HttpStatus.OK);
    }
//编辑活动信息
    @PutMapping("/editActivity")
    public ResponseEntity<EditActivityResponse> editActivity(@RequestBody EditActivityRequest request) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(request.getAdminToken());
        } catch (Exception e) {
            return new ResponseEntity<>(new EditActivityResponse("failure", "token解析失败"), HttpStatus.UNAUTHORIZED);
        }

        EditActivityResponse response = adminActivityService.editActivity(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //活动审核
    @PostMapping("/HandleActivity")
    public ResponseEntity<?> handleActivityReview(@RequestBody HandleActivityReviewRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        HandleActivityReviewResponse response = adminActivityService.handleActivityReview(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

}



